package org.example.dic.core.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.dic.core.entity.DicInfo;
import org.example.dic.core.entity.DicType;
import org.example.dic.core.form.UpdateDicInfoForm;
import org.example.dic.core.mapper.DicInfoMapper;
import org.example.dic.core.service.IDicInfoService;
import org.example.system.common.constant.BaseConstant;
import org.example.system.common.dto.dic.DicInfoDto;
import org.example.system.common.utils.JoeBeanCopyUtil;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 数据字典信息表  服务实现类
 * </p>
 *
 * @author Joey
 * @since 2021-05-13
 */
@Service
public class DicInfoServiceImpl extends ServiceImpl<DicInfoMapper, DicInfo> implements IDicInfoService {
    @Resource
    DicInfoMapper dicInfoMapper;

    @Cacheable(value = "cache1",key = "'dic:queryDicTextByKeys_'+#typeKey+'_'+#dicKeys")
    @Override
    public String queryDicTextByKeys(String typeKey, String dicKeys) {
        if(StrUtil.isEmpty(typeKey) || StrUtil.isEmpty(dicKeys)){
            return BaseConstant.STR_EMPTY;
        }
        List<String> dicKeyList = StrUtil.split(dicKeys, BaseConstant.SPITS_CHAR1);
        QueryWrapper qw = new QueryWrapper();
        qw.select("dicText");
        qw.eq("typeKey",typeKey);
        qw.in("dicKey",dicKeyList);
        List<DicInfo> dicList = dicInfoMapper.selectList(qw);
        if(dicList==null || dicList.size()==0){
            return BaseConstant.STR_EMPTY;
        }
        Set<String> dicTextSet = new HashSet<>(dicList.size());
        for(DicInfo item : dicList){
            dicTextSet.add(item.getDicText());
        }
        return CollUtil.join(dicTextSet, BaseConstant.SPITS_STR1);
    }

    @Cacheable(value = "cache1",key = "'dic:queryDicInfoList_'+#typeKey")
    @Override
    public List<DicInfoDto> queryDicInfoList(String typeKey) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("typeKey",typeKey);
        qw.orderByAsc("sortNum");
        List<DicInfo> dicList = dicInfoMapper.selectList(qw);
        return JoeBeanCopyUtil.create().copyList(dicList,DicInfoDto.class);
    }

    @Override
    public boolean existByTypeKeyAndDicKey(String typeKey, String dicKey, Long dicInfoId) {
        QueryWrapper qw = new QueryWrapper();
        qw.select("id");
        qw.eq("typeKey",typeKey);
        qw.eq("dicKey",dicKey);
        DicInfo dicInfo = dicInfoMapper.selectOne(qw);
        if(dicInfo==null){
            return false;
        }
        if(dicInfo!=null && dicInfo.getId().equals(dicInfoId)){
            return false;
        }
        return true;
    }

    @Override
    public void updateDicInfoByForm(UpdateDicInfoForm form) {
        JSONObject jsonObject = JSONUtil.parseObj(form);
        dicInfoMapper.updateDicInfo(jsonObject);
    }

    @Override
    public String queryIdPath(Long parentId) {
        if(parentId==null || parentId == BaseConstant.LONG_0){
            return null;
        }
        DicInfo dicInfo = dicInfoMapper.selectById(parentId);
        if(dicInfo == null){
            return null;
        }
        List<String> idList = new ArrayList<>();
        idList.add(dicInfo.getId().toString());
        String idPath = this.queryIdPath(dicInfo.getParentId());
        if(StrUtil.isNotEmpty(idPath)){
            idList.add(idPath);
        }
        return CollUtil.join(idList,BaseConstant.SPITS_STR4);
    }
}
